﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace Clotho.Time
{
    public class ChinaLunarCalendar : IFeature
    {
        public class Tag : ITag , IComparable
        {
            public int time_i;

            public Tag(Tag old) : base(old)
            {
                time_i = old.time_i;
            }
            public Tag(IFeature a, int time) : base(a)
            {
                time_i = time;
            }
            public int CompareTo(object obj)
            {
                if (obj is Itime)
                {
                    return this.time_i - (obj as Itime).time_i;
                }
                throw new NotImplementedException();
            }
            static string[] 天干 = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
            static string[] 地支 = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
            public override string ToString()
            {
                int quarter = time_i % 96;
                int day = time_i / 96;


                int month = (int)Math.Floor(day / 29.5306);
                int day_in_m = (int)Math.Ceiling(day - month * 29.5306);


                int year = (int)Math.Floor(month / (365.2422 / 29.5306));
                int month_in_y = (int)Math.Ceiling(month - year * (365.2422 / 29.5306));

                return string.Format("{0}{1}年{2}月{3}日 {4}时{5}刻",
                    天干[year % 10], 地支[year % 12], month_in_y, day_in_m,
                    地支[quarter / 8], quarter % 8);

            }
            public override object Clone()
            {
                return new Tag(this);
            }
            public override bool Equals(object obj)
            {
                if (ReferenceEquals(this, obj))
                {
                    return true;
                }

                if (ReferenceEquals(obj, null))
                {
                    return false;
                }
                return time_i.Equals((obj as Tag).time_i);
            }

            public override int GetHashCode()
            {
                return time_i;
            }
            public static bool operator ==(Tag left, Tag right)
            {
                if (ReferenceEquals(left, null))
                {
                    return ReferenceEquals(right, null);
                }

                return left.Equals(right);
            }
            public static bool operator !=(Tag left, Tag right)
            {
                return !(left == right);
            }

            public static bool operator <(Tag left, Tag right)
            {
                return ReferenceEquals(left, null) ? !ReferenceEquals(right, null) : left.CompareTo(right) < 0;
            }

            public static bool operator <=(Tag left, Tag right)
            {
                return ReferenceEquals(left, null) || left.CompareTo(right) <= 0;
            }

            public static bool operator >(Tag left, Tag right)
            {
                return !ReferenceEquals(left, null) && left.CompareTo(right) > 0;
            }

            public static bool operator >=(Tag left, Tag right)
            {
                return ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.CompareTo(right) >= 0;
            }
        }


        public override ITag create(Plot p)
        {
            return new Tag(this,2322412);
        }

        public override void lode(ITag tag, Control c)
        {
            throw new NotImplementedException();
        }

        public override ITag readTagForm(StreamReader sr,Plot p)
        {
            string[] sts = sr.ReadLine().Split(new char[] { ':' },2);
            int num = int.Parse(sts[0]);
            return new Tag(this, num);
        }

        public override void  writeTagTo(ITag tag, StreamWriter sw)
        {
            Tag time = tag as Tag;
            sw.WriteLine($"{time.time_i}:{time.ToString()}");
        }

        public override void paint(ITag tag, Graphics g, PointF location, Font f)
        {
            Tag time = tag as Tag;
            string str = time.ToString();
            SizeF str_size = g.MeasureString(str, f);

            SolidBrush brush = new SolidBrush(Color.White);
            g.FillRectangle(brush, new RectangleF(location, str_size));
            brush = new SolidBrush(Color.Black);
            g.DrawString(str, f, brush, location);
        }

        public override object Clone()
        {
            throw new NotImplementedException();
        }


        protected ChinaLunarCalendar(ChinaLunarCalendar old) : base(old)
        {
        }
        public ChinaLunarCalendar(Novel novel) : base(novel)
        {
        }
        public ChinaLunarCalendar(Novel novel, StreamReader sr) : base(novel, sr)
        {

        }
        public override void writeTo(StreamWriter sw)
        {
            base.writeTo(sw);
        }




    }
}
